// 收藏夹页面交互逻辑
document.addEventListener('DOMContentLoaded', () => {
    const favoriteGrid = document.querySelector('.favorites-grid');
    const emptyState = document.querySelector('.empty-state');
    const genreFilter = document.getElementById('genre-filter');
    const sortFilter = document.getElementById('sort-filter');

    // 检查收藏夹是否为空
    function checkEmptyState() {
        const gameCards = favoriteGrid.querySelectorAll('.game-card');
        const visibleCards = Array.from(gameCards).filter(card => 
            card.style.display !== 'none'
        );
        
        if (visibleCards.length === 0) {
            emptyState.style.display = 'flex';
            favoriteGrid.style.display = 'none';
        } else {
            emptyState.style.display = 'none';
            favoriteGrid.style.display = 'grid';
        }
    }

    // 初始化检查
    checkEmptyState();

    // 游戏类型筛选
    genreFilter.addEventListener('change', (e) => {
        const selectedGenre = e.target.value.toLowerCase();
        const gameCards = favoriteGrid.querySelectorAll('.game-card');

        gameCards.forEach(card => {
            const gameGenre = card.querySelector('.game-genre').textContent.toLowerCase();
            if (selectedGenre === 'all' || gameGenre.includes(selectedGenre)) {
                card.style.display = 'block';
            } else {
                card.style.display = 'none';
            }
        });

        checkEmptyState();
    });

    // 排序方式
    sortFilter.addEventListener('change', (e) => {
        const sortMethod = e.target.value;
        const gameCards = Array.from(favoriteGrid.querySelectorAll('.game-card'));

        gameCards.sort((a, b) => {
            switch (sortMethod) {
                case 'recent':
                    // 获取最后游玩时间（示例中使用文本，实际应该使用时间戳）
                    const timeA = a.querySelector('.last-played').textContent;
                    const timeB = b.querySelector('.last-played').textContent;
                    return timeA.localeCompare(timeB);
                case 'playtime':
                    // 获取游戏时长（示例中提取数字）
                    const playTimeA = parseInt(a.querySelector('.playtime').textContent.match(/\d+/)[0]);
                    const playTimeB = parseInt(b.querySelector('.playtime').textContent.match(/\d+/)[0]);
                    return playTimeB - playTimeA;
                case 'rating':
                    const ratingA = parseFloat(a.querySelector('.game-rating').textContent);
                    const ratingB = parseFloat(b.querySelector('.game-rating').textContent);
                    return ratingB - ratingA;
                default:
                    return 0;
            }
        });

        // 重新排序DOM元素
        gameCards.forEach(card => favoriteGrid.appendChild(card));
    });

    // 移除收藏按钮事件
    favoriteGrid.addEventListener('click', (e) => {
        if (e.target.classList.contains('btn-secondary')) {
            const gameCard = e.target.closest('.game-card');
            const gameName = gameCard.querySelector('h3').textContent;

            // 添加淡出动画
            gameCard.style.transition = 'opacity 0.3s ease, transform 0.3s ease';
            gameCard.style.opacity = '0';
            gameCard.style.transform = 'scale(0.9)';

            // 等待动画完成后移除卡片
            setTimeout(() => {
                gameCard.remove();
                checkEmptyState();
                // TODO: 调用后端API移除收藏
                console.log(`已移除收藏：${gameName}`);
            }, 300);
        }
    });

    // 开始游戏按钮事件
    favoriteGrid.addEventListener('click', (e) => {
        if (e.target.classList.contains('btn-primary')) {
            const gameCard = e.target.closest('.game-card');
            const gameName = gameCard.querySelector('h3').textContent;

            // 添加点击反馈动画
            const button = e.target;
            button.style.transform = 'scale(0.95)';
            setTimeout(() => {
                button.style.transform = '';
            }, 100);

            // TODO: 跳转到游戏页面或启动游戏客户端
            console.log(`启动游戏：${gameName}`);
        }
    });
});
